package view;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JWindow;

import controller.Engine;


@SuppressWarnings("serial")
public class SplashScreen extends JWindow implements Observer{
	//static int durata=2000;
	static JWindow w;
	static JProgressBar progressbar = new JProgressBar(0,100);
	static boolean flag;
	static double load=0;
	static double add = 0;
	static int pausa=0; //imposta il tempo di pausa nel caricamento

	public SplashScreen(){
		w=this;
		MostraSplash();
	}

	public void MostraSplash() {
		JPanel contenitore = new JPanel();
		JPanel image = new JPanel();
		JPanel bar = new JPanel();
		contenitore.setBackground(Color.black);

		Dimension schermo = Toolkit.getDefaultToolkit().getScreenSize();
		int lunghezza = (int)(schermo.width/1.2);
		int altezza = (int)(schermo.height/1.2);
		String Screen = "SplashScreenHD.jpg";
		if (schermo.width>=1920 && schermo.height>=1200){
			lunghezza = 1920;
			altezza = 1200;
		}
		int x = (schermo.width-lunghezza)/2;
		int y = (schermo.height-altezza)/2;

		image.add(Engine.setImage(Screen, lunghezza-10, altezza-40));
		image.setBackground(Color.darkGray);

		//progressbar.setMaximumSize(new Dimension(lunghezza-10, 10));
		progressbar.setPreferredSize(new Dimension(lunghezza-10, 20));
		progressbar.setBackground(new Color(0,0,0));
		progressbar.setString("Inizio caricamento LoAD");
		progressbar.setStringPainted(true);
		bar.add(progressbar);

		contenitore.setLayout(new BoxLayout(contenitore, BoxLayout.Y_AXIS));
		contenitore.add(image);
		contenitore.add(bar);

		setEnabled(false);

		setBounds(x,y,lunghezza,altezza);
		add(contenitore);
		pack();
		requestFocus();

		//Colorora Red = new Color(160, 20, 20,255);
		//contenitore.setBorder(BorderFactory.createLineBorder(oraRed, 10));

		Engine.startModel();
		Engine.modelloDb.addObserver(this);
		setVisible(true);
		Engine.modelloDb.uppdater();

	}

	public static void disableSplash(){
		w.setVisible(false);
		w.dispose();
	}

	@Override
	public void update(Observable o, Object arg) {
		String txt = ""+progressbar.getValue()+"%";
		//controller.modelloDb.uppdater();
		if (load<=100)
			if (load==0){
				progressbar.setString(txt+" Connessione con il database");
				try {
					Thread.sleep(pausa);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				setProgress(1);
			}
			else
				if (load==1){
					progressbar.setString(txt+" Connessione con il database stabilita");
					try {
						Thread.sleep(pausa);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					progressbar.setString(txt+" Caricamento dati dal database");
					Engine.modelloDb.loadAllContacts();
				}
				else
					if (load>1 && load<60){
						progressbar.setString(txt+" Caricamento dati dal database");
						try {
							Thread.sleep(pausa);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					else
						if (load>=60 && load<=71){
							progressbar.setString(txt+" Caricamento dati dal database completato");
							try {
								Thread.sleep(pausa);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
							progressbar.setValue(71);
							setProgress(1);
						}
						else
							if (load>=72 && load<99){
								progressbar.setString(txt+" Caricamento interfaccia grafica");

								try {
									Thread.sleep(pausa/5);
								} catch (InterruptedException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
								progressbar.setString(txt+" Caricamento interfaccia grafica");
								setProgress(1);
							}
							else
								if (load==99){
									progressbar.setString(txt+" Caricamento interfaccia grafica completato");
									setProgress(1);
									try {
										Thread.sleep(500);
									} catch (InterruptedException e) {
										// TODO Auto-generated catch block
										e.printStackTrace();
									}

								}
								else
									if (load==100){
										progressbar.setString(txt+" Caricamento di LoAD terminato");
										personalize.lookandfeel2.createAndShowGUI();
										Engine.startLogin();
									}
		w.repaint();
	}

	public static void setContactCount(int opz) {
		add = 70/opz;
	}

	public static void setContactProgress(){
		load+=add;
		progressbar.setValue((int)load);
		progressbar.setForeground(new Color((int)(255-(load*2)),(int)(load*2),0));
		Engine.modelloDb.uppdater();
	}

	public static void setProgress(int opz) {
		load+=opz;
		progressbar.setValue((int)load);
		progressbar.setForeground(new Color((int)(255-(load*2)),(int)(load*2),0));
		Engine.modelloDb.uppdater();
	}


}

